package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper,CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckItemService checkItemService;

    //检查组查询
    @Override
    public Boolean saveCheckGroupInfo(CheckGroupDTO checkGroupDTO) {

        //获取CheckGroupDTO的id
        Integer id = checkGroupDTO.getId();
        //判断id是否存在
        if (StringUtils.isNotBlank(String.valueOf(id))) {
            //id存在说明是修改操作，那就物理删除中间表，再重新添加
            baseMapper.deleteCheckItemInfoByGroupId(id);
        }

        //完成检查组的添加或修改
        boolean flag = saveOrUpdate(checkGroupDTO);
        //mp获取检查组主键
        Integer gid = checkGroupDTO.getId();
        //获取检查项id数组
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if (checkitemIds != null && checkitemIds.length > 0) {
            //遍历录入中间表
            for (Integer checkitemId : checkitemIds) {
                //添加和修改的 中间表数据录入
                baseMapper.addCheckGroupIdAndCheckItemId(gid,checkitemId);
            }
        }
        return flag;
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page<CheckGroup> page = null;
        //构建条件
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper();
        //查询有效数据
        queryWrapper.eq("is_delete", 0);
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            //无条件查询
           page= page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }else {
            //有条件查询
            queryWrapper.like("name", queryPageBean.getQueryString());
           page= page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }


    //逻辑删除
    @Override
    public Boolean updateCheckGroupById(int id) {
        //创建检查项对象，并设置id
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        //修改is_delete字段为1,是他变为无效
        checkGroup.setIs_delete(1);
//        逻辑删除的原理：就是查询的时候加上where is_delete=0
//        的条件，凡是不等于0的都查询不出来(不显示)，
//        即视为逻辑删除(数据失效,数据库还存在)，
//        所以但凡查询都要加上where is_delete=0的条件
        return updateById(checkGroup);
    }


    //检查组修改数据回显
    @Override
    public CheckGroupVO findCheckItemInfoByGroupId(int gid) {
        //查询所有检查项
        List<CheckItem> checkItemList = checkItemService.list();
        //查询当前，检查组所对应的检查项的id集合
        List<Integer> checkitemIds = baseMapper.findCheckItemInfoByGroupId(gid);
        //设置数据
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(checkItemList);
        checkGroupVO.setCheckitemIds(checkitemIds);
        return checkGroupVO;
    }

}
